home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / e / amigae21b.lha / Amiga_E_v2.1b / Sources / Other / GetLength.e < prev    next >
Text File  |  1993-02-13  |  2KB  |  89 lines

  1. /* GetLength.e
  2.  * Gets the length of the specified file and prints it or put it in an
  3.  * environment variable.
  4.  *
  5.  * Public Domain by Diego Caravana.
  6.  *
  7.  *
  8.  * Thanks to Wouter van Oortmerssen for his VERY GOOD work, hoping that he
  9.  * will continue to improve E language !
  10.  *
  11.  * Hey, consider to learn E !!!
  12.  *
  13.  */
  14.  
  15. /* TAB=4 */
  16.  
  17. OPT OSVERSION=37
  18.  
  19. MODULE 'dos/var', 'utility/tagitem'
  20.  
  21. CONST ARGS_NUM=4, BUFLEN=11
  22. ENUM  ARG_FILE, ARG_VAR, ARG_GLOBAL, ARG_STDOUT
  23. ENUM  ERR_NONE, ERR_WRONGARGS, ERR_BADKEYS, ERR_NOVAR
  24.  
  25. /* global definitions are cleared on entry */
  26. DEF args[ARGS_NUM]:ARRAY OF LONG, buf[BUFLEN]:ARRAY OF CHAR
  27.  
  28. RAISE    ERR_WRONGARGS    IF ReadArgs()=NIL,
  29.         ERR_NOVAR        IF SetVar()=NIL
  30.  
  31. PROC main() HANDLE
  32. DEF rdargs=NIL, len, fmt, fmtarg
  33.  
  34.     /* version string, as C= says... */
  35.     VOID '$VER: GetLength 1.0 (10.02.93) by Diego Caravana'
  36.  
  37.     /* get user arguments from command line */
  38.     rdargs := ReadArgs('FILENAME/A,VAR=VARIABLE/K,GLOBAL/S,STDOUT/S', args, 0)
  39.  
  40.     /* do not allow the use of VAR/GLOBAL and STDOUT keywords
  41.      * because in this way the user can print the result directly on stdout
  42.      * without being bored by env variables
  43.      */
  44.     IF ((StrLen(args[ARG_VAR]) <> 0) OR (args[ARG_GLOBAL]=-1)) AND
  45.             (args[ARG_STDOUT]=-1) THEN Raise(ERR_BADKEYS)
  46.  
  47.     len:=FileLength(args[ARG_FILE])
  48.  
  49.     IF args[ARG_STDOUT]=-1        /* choose the way */
  50.         WriteF('\d', len)
  51.     ELSE
  52.         /* default name for environment variable */
  53.         IF StrLen(args[ARG_VAR]) = 0 THEN args[ARG_VAR] := 'GetLengthResult'
  54.  
  55.         /* example  of inline assembler; see the RawDoFmt Autodoc */
  56.         fmt:='%ld'; fmtarg:=[len]
  57.         MOVE.L    fmt,A0
  58.         MOVE.L    fmtarg,A1
  59.         LEA.L    stuffChar(PC),A2
  60.         MOVE.L    buf,A3
  61.         MOVE.L    4,A6
  62.         JSR        -$20A(A6)        /* RawDoFmt */
  63.  
  64.         SetVar(args[ARG_VAR], buf, StrLen(buf),
  65.                 IF args[ARG_GLOBAL]=-1 THEN GVF_GLOBAL_ONLY ELSE GVF_LOCAL_ONLY)
  66.     ENDIF
  67.  
  68.     /* let's exit cleanly */
  69.     Raise(ERR_NONE)
  70.  
  71.     /* needed for RawDoFmt() (put here bucause is unreachable by program flux) */
  72. stuffChar:
  73.     MOVE.B    D0,(A3)+
  74.     RTS
  75.  
  76. EXCEPT
  77.     IF rdargs THEN FreeArgs(rdargs)
  78.  
  79.     SELECT exception
  80.         CASE ERR_NONE;        CleanUp(0)
  81.         CASE ERR_BADKEYS
  82.             WriteF('*** no simultaneous VAR/GLOBAL and STDOUT keywords.\n')
  83.             CleanUp(10)
  84.         DEFAULT
  85.             PrintFault(IoErr(), '*** DOS Error')
  86.             CleanUp(10)
  87.     ENDSELECT
  88. ENDPROC
  89.